﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using System.Xml.Linq;
using ProtoType.DataStorage;
using ProtoType.ProtoTypeService;

namespace ProtoType
{
    public partial class DetailLayoutType : PhoneApplicationPage
    {
        ProtoTypeServiceClient client;
        AppSettings apps = new AppSettings();
        DataStructureStore dss = new DataStructureStore();
        string selectedfid;

        public DetailLayoutType()
        {
            InitializeComponent();
            client = new ProtoTypeServiceClient();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (NavigationContext.QueryString.TryGetValue("fid", out selectedfid))
            {
            }
        }

        private void lstboxDetailLayout_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstboxDetailLayout.SelectedIndex != -1)
            {
                ListBoxItem lstboxitem = lstboxDetailLayout.SelectedItem as ListBoxItem;

                if (apps.IsolatedStorageSavingSetting)
                {
                    dss.SetformDetailLayout(selectedfid, lstboxitem.Content.ToString());
                    NavigationService.Navigate(new Uri("/DetailLayOutControls.xaml?selectedfid=" + selectedfid, UriKind.Relative));
                }
                if (apps.WebServiceSavingSetting)
                {
                    client.SetformDetailLayoutAsync(selectedfid, lstboxitem.Content.ToString());
                    client.SetformDetailLayoutCompleted += new EventHandler<SetformDetailLayoutCompletedEventArgs>(client_SetformDetailLayoutCompleted);
                }
            }
        }

        void client_SetformDetailLayoutCompleted(object sender, SetformDetailLayoutCompletedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/DetailLayOutControls.xaml?selectedfid=" + selectedfid, UriKind.Relative));
        }
    }
}